/**
 * 
 */
package org.hotpotmaterial.codegenn2.config.security.entrypoint;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hotpotmaterial.codegenn2.dto.LoginDTO;
import org.hotpotmaterial.codegenn2.exception.TempDisableException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.AuthenticationEntryPoint;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * @author cakydeveloper
 *
 */
@Slf4j
public class UnauthorizedEntryPoint implements AuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException {
		SecurityContextHolder.clearContext();
		// 设置ContentType
		response.setContentType("application/json;charset=UTF-8");
		// 设置HTTP协议CODE
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		// 初始化RESPONSE消息体的对象
		LoginDTO result = new LoginDTO();
		if (authException instanceof TempDisableException) {
			TempDisableException tempFailed = (TempDisableException) authException;
			// 设置HTTP协议CODE
			response.setStatus(HttpServletResponse.SC_FORBIDDEN);
			result.setSuccess(true);
			// 返回状态403
			result.setStatus(HttpServletResponse.SC_FORBIDDEN);
			// 设置错误消息
			result.setErrorMessage(authException.getMessage());
			result.setData(tempFailed.getToken());
		} else {
			// 设置HTTP协议CODE
			response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
			result.setSuccess(true);
			// 返回状态403
			result.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
			// 设置错误消息
			result.setErrorMessage(authException.getMessage());
		}
		// 写入RESPONSE
		PrintWriter out = null;
		// 转JSON
		ObjectMapper mapper = new ObjectMapper();
		// 将RESULT对象转为JSON字符串
		String resultJson = mapper.writeValueAsString(result);
		try {
			// 写入RESPONSE
			out = response.getWriter();
			out.append(resultJson);
		} catch (IOException e) {
			log.error("", e);
		} finally {
			if (out != null) {
				// 关闭WRITER
				out.close();
			}
		}
	}

}
